The AVPlayer class provides audio and video playback capabilities with support for playback control, looping, event callbacks, and metadata retrieval.
You can use setSource() to set a media source (either a local file or a remote URL) and call play() to start playback.
Example usage of AVPlayer:
volume: numberControls the playback volume, ranging from 0.0 (muted) to 1.0 (maximum).
duration: DurationInSecondsThe total duration of the media in seconds.
This value will be 0 until the media is fully loaded.
currentTime: DurationInSecondsThe current playback time in seconds. You can set this value to seek to a specific time.
rate: numberControls the playback speed.
A value of 1.0 is normal speed; values below 1.0 slow down playback, and values above 1.0 speed it up.
timeControlStatus: TimeControlStatusIndicates the current playback state. Possible values:
paused: playback is pausedwaitingToPlayAtSpecifiedRate: waiting for conditions to start (e.g., buffering)playing: currently playingnumberOfLoops: numberSets how many times the media will loop.
0: no loopingsetSource(filePathOrURL: string): booleanSets the media source for playback. Accepts a local file path or a remote URL.
Returns:
true if successfully setfalse if setting failedplay(): booleanStarts playback of the current media.
Returns:
true if playback started successfullyfalse if it failed to startpause()Pauses the current playback.
stop()Stops playback and resets the position to the beginning.
dispose()Releases all player resources and removes observers. Call this method when the player is no longer needed to avoid memory leaks.
loadMetadata(): Promise<AVMetadataItem[] | null>Loads detailed metadata for the current media file.
Returns:
AVMetadataItem objectsnull if no metadata is available or no source has been setExample:
loadCommonMetadata(): Promise<AVMetadataItem[] | null>Loads the common metadata of the current media, where each AVMetadataItem provides a commonKey that can be used across media formats.
Example:
onReadyToPlay?: () => voidCalled when the media is ready for playback.
onTimeControlStatusChanged?: (status: TimeControlStatus) => voidCalled when the playback state changes, such as from “waiting” to “playing.”
onEnded?: () => voidCalled when playback finishes.
onError?: (message: string) => voidCalled when an error occurs during playback. Receives an error message as an argument.
AVPlayer relies on the system’s shared audio session.
You can configure it using SharedAudioSession to ensure correct playback behavior.
Handling interruptions (such as phone calls):
Resource Management
Always call dispose() after playback to release system resources.
Error Handling
Implement the onError callback to handle playback issues gracefully (e.g., network failures).
Interruption Management Use audio session interruption listeners to pause and resume playback smoothly.
UI State Updates
Use onTimeControlStatusChanged to update your UI when the playback state changes.
Metadata Usage
Use loadCommonMetadata() to retrieve general information such as title, artist, or album artwork for display in your app’s UI.